//
//  YingVC.swift
//  全国高等学校查询
//
//  Created by 高宇超 on 6/23/17.
//  Copyright © 2017 Yuchao. All rights reserved.
//

import UIKit
import Crashlytics

class YingVC: UIViewController {

    @IBOutlet weak var logo: UIImageView!
    @IBOutlet weak var shareBtn: UIButton!
    @IBOutlet weak var notifyLabel: UILabel!
    
    var logoOrigin: CGPoint!
    var logoSize: CGSize!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        Answers.logCustomEvent(withName: "Ying View Loaded", customAttributes: ["time": DateFormatter().string(from: Date()), "device": UIDevice.current.modelName])

        self.logo.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(logoTapped(sender:))))
        
        self.logoOrigin = self.logo.frame.origin
        self.logoSize = self.logo.frame.size

//        self.notifyLabel.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(dismissNotifiTap(sender:))))
//        self.notifyLabel.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(dismissNotifiPan(sender:))))

    }
    
    override func viewWillAppear(_ animated: Bool) {
        logoAnimation()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        logo.layer.removeAllAnimations()
    }

    func dismissNotifiTap(sender: UITapGestureRecognizer) {
        UIView.dismissNotify(label: notifyLabel)
    }
    
    func dismissNotifiPan(sender: UITapGestureRecognizer) {
        UIView.dismissNotify(label: notifyLabel)
    }
    
    func logoTapped(sender: UISegmentedControl) {
        Answers.logCustomEvent(withName: "Ying Logo Clicked", customAttributes: ["time": DateFormatter().string(from: Date()), "device": UIDevice.current.modelName])
        alertOkay(title: "版本号", msg: "v1.0", action: { _ in })
    }
    
    func logoAnimation() {
        let rand = Float(YingVC.rand(max: 120, min: 80)) / 100.0
        UIView.animate(withDuration: TimeInterval(rand), delay: 0, options: [.curveEaseInOut, .allowUserInteraction], animations: {
            self.logo.frame.size.width = self.logoSize.width + 12
            self.logo.frame.size.height = self.logoSize.height + 12
            self.logo.frame.origin.x = self.logoOrigin.x - 6
            self.logo.frame.origin.y = self.logoOrigin.y - 6
        }, completion: {
            finished in
            if finished {
                let rand = Float(YingVC.rand(max: 120, min: 80)) / 100.0
                UIView.animate(withDuration: TimeInterval(rand), delay: 0, options: [.curveEaseInOut, .allowUserInteraction], animations: {
                    self.logo.frame.size.width = self.logoSize.width - 12
                    self.logo.frame.size.height = self.logoSize.height - 12
                    self.logo.frame.origin.x = self.logoOrigin.x + 6
                    self.logo.frame.origin.y = self.logoOrigin.y + 6
                }, completion: {
                    finished in
                    if finished {
                        self.logoAnimation()
                    }
                })
            }
        })
    }
    
    static func rand(max: Int, min: Int) -> Int {
        return Int(arc4random_uniform(UInt32(max - min)) + UInt32(min))
    }
    
    @IBAction func shareBtnPressed(_ sender: Any) {
        let text = "小鹰高校查询 - 鉴别虚假大学利器，快速查询高校，点击下载"
        let image = UIImage(named: "logo60")!
        let url = NSURL(string: "https://itunes.apple.com/app/superboard/id1252056445")!
        // set up activity view controller
        let activityViewController = UIActivityViewController(activityItems: [text, image, url], applicationActivities: nil)
        activityViewController.popoverPresentationController?.sourceView = self.view // so that iPads won't crash
        
        // exclude some activity types from the list (optional)
        activityViewController.excludedActivityTypes = [
            UIActivityType.postToTwitter,
            UIActivityType.postToTencentWeibo,
            UIActivityType.postToFlickr,
            UIActivityType.postToVimeo,
            UIActivityType.postToFacebook,
            UIActivityType.assignToContact,
            UIActivityType.openInIBooks
        ]
        
        self.present(activityViewController, animated: true, completion: nil)
    }
    
    @IBAction func goodBtnPressed(_ sender: Any) {
        let url = URL(string: "itms-apps://itunes.apple.com/app/id1252056445")
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(url!, options: [:], completionHandler: nil)
        } else {
            // Fallback on earlier versions
            UIApplication.shared.openURL(url!)
        }
    }
    
    @IBAction func badBtnPressed(_ sender: Any) {
        let alertController = UIAlertController(title: "", message: "如果有任何意见或建议，欢迎发送邮件到作者邮箱 yc.g@qq.com 进行吐槽。", preferredStyle: .alert)
        let copy = UIAlertAction(title: "复制邮箱", style: .default, handler: {
            _ in
            UIPasteboard.general.string = "yc.g@qq.com"
            UIView.notifyc(label: self.notifyLabel, text: "已复制到剪贴板")
        })
        let cancel = UIAlertAction(title: "取消", style: .cancel, handler: nil)
        alertController.addAction(copy)
        alertController.addAction(cancel)
        self.present(alertController, animated: true, completion: nil)
    }
    
    
    @IBAction func moreBtnPressed(_ sender: Any) {
        let alertController = UIAlertController(title: "喜欢这款应用吗？", message: "", preferredStyle: .actionSheet)
        let share = UIAlertAction(title: "分享应用❤️", style: .default, handler: {
            _ in
            let url = URL(string: "https://itunes.apple.com/app/superboard/id1252056445")!
            // set up activity view controller
            let activityViewController = UIActivityViewController(activityItems: [url], applicationActivities: nil)
            activityViewController.popoverPresentationController?.sourceView = self.view // so that iPads won't crash
            
            // exclude some activity types from the list (optional)
            activityViewController.excludedActivityTypes = [
                UIActivityType.airDrop,
                UIActivityType.postToFacebook,
                UIActivityType.postToWeibo,
                UIActivityType.postToTencentWeibo,
                UIActivityType.assignToContact,
                UIActivityType.message,
                UIActivityType.mail
            ]
            
            // present the view controller
            self.present(activityViewController, animated: true, completion: nil)
        })
        let good = UIAlertAction(title: "给予好评👍", style: .default, handler: {
            _ in
            let url = URL(string: "itms-apps://itunes.apple.com/app/id1252056445")
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(url!, options: [:], completionHandler: nil)
            } else {
                // Fallback on earlier versions
                UIApplication.shared.openURL(url!)
            }
        })
        let advice = UIAlertAction(title: "吐槽一下👎", style: .destructive, handler: {
            _ in
            let alertController = UIAlertController(title: "", message: "如果有任何意见或建议，欢迎发送邮件到作者邮箱 yc.g@qq.com 进行吐槽。", preferredStyle: .alert)
            let copy = UIAlertAction(title: "复制地址", style: .default, handler: {
                _ in
                UIPasteboard.general.string = "yc.g@qq.com"
                UIView.notifyc(label: self.notifyLabel, text: "已复制到剪贴板")
            })
            let cancel = UIAlertAction(title: "取消", style: .cancel, handler: nil)
            alertController.addAction(copy)
            alertController.addAction(cancel)
            self.present(alertController, animated: true, completion: nil)
        })
        let cancel = UIAlertAction(title: "取消", style: .cancel, handler: nil)
        alertController.addAction(share)
        alertController.addAction(good)
        alertController.addAction(advice)
        alertController.addAction(cancel)
        self.present(alertController, animated: true, completion: nil)
    }

}
